﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SalesOrderEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (Request.QueryString["SalesOrderID"] != null)
                {
                    if (Request.QueryString["SalesOrderID"].ToString() != null)
                    {
                        //用一个隐藏Label保存传递过来的ID
                        this.lblSalesOrderID.Text = Request.QueryString["SalesOrderID"].ToString();
                    }
                    else
                    {
                        mk.Alert("参数错误！");
                    }
                }
                else
                {
                    mk.Alert("参数错误！");
                }

                //绑定页面基本数据
                this.BindData();
            }
        }
        private void BindData()
        {
            //给日期输入框添加只读属性，不能在属性里直接设置，不然会取不到值
            //this.txtDateDelivery.Attributes["readonly"] = "true";
            //this.txtDateInspect.Attributes["readonly"] = "true";
            //this.txtDateInstall.Attributes["readonly"] = "true";
            //this.txtDateOrder.Attributes["readonly"] = "true";

            var bll = mk.l.Sales_SalesOrderHeader.ToList();
            Sales_SalesOrderHeader m = bll.Where(y => y.SalesOrderID == (int.Parse(this.lblSalesOrderID.Text))).FirstOrDefault();


            if (m != null)
            {
                //不能修改已经结账完毕的订单
                var v = mk.l.Accounts_Users.ToList();
                //先判断是否是本人添加的订单，不能修改别人添加的订单
                Accounts_Users currentUser = v.FirstOrDefault();
                if (m.Designer == currentUser.UserName)
                {
                    this.lblCustomerID.Text = m.CustomerID.ToString();
                    //显示客户信息
                    var bllC = mk.l.Sales_Customer.ToList();
                    Sales_Customer mC = bllC.Single(h => h.CustomerID == (int.Parse(this.lblCustomerID.Text)));
                    this.txtCustomerName.Text = mC.CustomerName;
                    this.txtCellPhone.Text = mC.CellPhone;
                    this.txtPhone.Text = mC.Phone;

                    this.txtAddress.Text = m.Address;
                    this.txtDateDelivery.Date = m.DateDelivery;
                    this.txtDateInspect.Date = m.DateInspect;
                    this.txtDateInstall.Date = m.DateInstall;
                    this.txtDateOrder.Date = m.DateOrder;
                    this.txtSalesOrderNumber.Text = m.SalesOrderNumber;

                    this.txtNotesInstall.Text = m.NotesInstall;
                    this.txtNotesManufacture.Text = m.NotesManufacture;
                    this.txtNotesOrder.Text = m.NotesOrder;
                    this.lblContractAmount.Text = m.ContractAmount.ToString();
                    this.lblTotalDue.Text = m.TotalDue.ToString();
                    this.lblSubTotal.Text = m.SubTotal.ToString();
                    this.lblBrokerage.Text = m.Brokerage.ToString();
                    this.lblDesignerUserName.Text = currentUser.UserName;
                    this.lblDesignerTrueName.Text = currentUser.TrueName;
                    //如果订单已经提交 则隐藏提交按钮
                    if (m.StatusSubmit == "3")
                    {
                        this.btnOK.Enabled = false;
                        this.btnOK.Visible = false;
                    }
                }
                else
                {
                    mk.Alert("不能修改他人添加的订单！");
                }

            }
            else
            {
                mk.Alert("参数错误！");
            }

        }
        protected void btnOK_Click(object sender, EventArgs e)
        {

            //修改客户信息
            var bllC = mk.l.Sales_Customer.ToList();
            Sales_Customer mC = bllC.Single(h => h.CustomerID == (int.Parse(this.lblCustomerID.Text)));
            mC.CellPhone = this.txtCellPhone.Text;
            mC.CustomerName = this.txtCustomerName.Text;
            mC.ModifiedDate = System.DateTime.Now;
            mC.Phone = this.txtPhone.Text;

            var bll = mk.l.Sales_SalesOrderHeader.ToList();
            Sales_SalesOrderHeader m = bll.Single(h => h.SalesOrderID == (int.Parse(this.lblSalesOrderID.Text)));
            if (m != null)
            {
                m.Address = this.txtAddress.Text;
                //m.CustomerID = int.Parse(this.lblCustomerID.Text);
                m.DateDelivery = Convert.ToDateTime(this.txtDateDelivery.Date);
                m.DateInspect = Convert.ToDateTime(this.txtDateInspect.Date);
                m.DateInstall = Convert.ToDateTime(this.txtDateInstall.Date);
                m.DateOrder = Convert.ToDateTime(this.txtDateOrder.Date);
                //m.Designer = this.lblDesignerUserName.Text;
                m.ModifiedDate = System.DateTime.Now;
                m.NotesInstall = mk.InputText(this.txtNotesInstall.Text, 200);
                m.NotesManufacture = mk.InputText(this.txtNotesManufacture.Text, 200);
                m.NotesOrder = mk.InputText(this.txtNotesOrder.Text, 200);
                m.SalesOrderNumber = mk.InputText(this.txtSalesOrderNumber.Text, 30);
                //初始添加，状态位设置为 1
                //m.Status = "1";
                try
                {
                    mk.l.SubmitChanges();
                    mk.Alert("修改订单成功！");
                }
                catch (Exception ex)
                {
                    mk.Alert("修改订单失败！错误信息：" + ex.Message);
                }
            }
            else
            {
                mk.Alert("订单不存在或已被删除！");
            }

        }

    }
}